package com.sunday.common.base;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.sunday.common.event.EventBus;
import com.sunday.common.volley.AuthFailureError;
import com.sunday.common.volley.Request;
import com.sunday.common.volley.Response;
import com.sunday.common.volley.toolbox.StringRequest;
import com.sunday.common.widgets.loadingdialog.LoadingDialog;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2015/6/15.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected  static final String TAG = "BaseActivity";

    /**
     * 常用成员变量
     */
    protected Intent intent;
    protected Context mContext;


    //标识是否显示对话框
    protected boolean progressShow;
    protected LoadingDialog progressDialog;
    protected boolean isFinish = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        MobclickAgent.openActivityDurationTrack(false);
//		MobclickAgent.setAutoLocation(true);
//		MobclickAgent.setSessionContinueMillis(1000);

        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType. E_UM_NORMAL);

    }

    protected void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    protected void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    protected  void showLoadingDialog(){
        progressShow = true;
        if(progressDialog==null){
            progressDialog = new LoadingDialog(mContext);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    progressShow = false;
                }
            });
        }
        if(progressDialog!=null&&!progressDialog.isShowing()){
            progressDialog.show();
        }
    }

    protected void dissMissDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(mContext);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(mContext);
    }

    public void back(View v){
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        isFinish = true;
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);//反注册EventBus
        }
    }

    protected StringRequest post(String url ,final HashMap<String,String> maps,
                        Response.ErrorListener errorListener,
                        Response.Listener response){
        StringRequest request = new StringRequest(Request.Method.POST, url,response,errorListener){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return maps;
            }
        };
        return request;
    }


}
